/**
 * Javascript function to display Seagate calendar with company workweek
 * Please keep these lines on when you copy the source
 * Author: Phuwarin Supawarapong, 14 July 2005
 * Last update: 5 Aug 2007
 * Version: 1.4.0
 *
 * Example:
 * <HEAD>
 *    <script src="./scripts/seagateww.js"></script>
 *    <script src="./scripts/seagateCalendar.js"></script>
 * </HEAD>
 * <BODY>
 *    <!-- display overlap day -->
 *    <script language='JavaScript'>
 *       document.write(getCalendar(true));
 *    </script><br>
 *    <!-- do not display overlap day -->
 *    <script language='JavaScript'>
 *       document.write(getCalendar());
 *    </script>
 * </BODY>
 */
 
<!-- Begin
//  DECLARE AND INITIALIZE VARIABLES
var Calendar = new Date();                // initial calendar at current date

var year = Calendar.getFullYear();	         // get year
var month = Calendar.getMonth();        // get month (0-11)
var today = Calendar.getDate();           // get day (1-31)
var weekday = Calendar.getDay();        // get day of week (1-7)
var currentMonth = month;
var currentYear = year;
var currentDay = today;

var DAYS_OF_WEEK = 7;                 // max number of days in a week
var DAYS_OF_MONTH = 31;             // max number of days in a month
var display_overlap_day = false;

function getDayOfWeek(index) {
  var day_of_week = new Array('Sa','Su','Mo','Tu','We','Th','Fr','Wk');
  return day_of_week[index];
}

function getMonthName(month) {
  var month_of_year = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
  return month_of_year[month];
}

function createCalendar() {
  var calBody = document.getElementById("calendarID");
  calBody.innerHTML  = getCalendar(display_overlap_day);
}

function nextMonth() {
  month++;
  if (month > 11)
  {
	month -= 12;
	year++;
  }  
  createCalendar();
}

function lastMonth() {
  month--;
  if (month < 0)
  {
	month += 12;
	year--;
  }  
  createCalendar();
}

function getDayTitle() {
  cal = '<tr bgcolor=#E0E0E0>';
  for (index=0; index < (DAYS_OF_WEEK+1); index++)
  {
    cal += '<td align=center width=27>';
    // bold today's day of week
    if (((((weekday+1) == index)&&(index != 7)) || ((weekday == 6)&&(index == 0))) && (month == currentMonth) && (year == currentYear))
      cal += '<b>' + getDayOfWeek(index) + '</b>';
    else
      cal += getDayOfWeek(index);
    cal += '</td>';
  }
  cal += '</tr>';
  return cal;
}

function getCalendarBody() {
  Calendar.setDate(1);                     // set the calendar day at '1'
  Calendar.setMonth(month);             // set the calendar month
  Calendar.setYear(year);                 // set the calendar year
  day_of_week = Calendar.getDay();   // get the day of the week

  cal = '';
  if (day_of_week != 6) {
    Calendar.setDate(Calendar.getDate() - Calendar.getDay() - 1);
    cal = '<tr bgcolor=#FFFFFF>';
    // fill overlap day of previous month
    for (index=0; index < day_of_week+1; index++) {
      day = (display_overlap_day) ? Calendar.getDate() : '';
      cal += '<td align=center><font color="gray">' + day + '</font></td>';
      Calendar.setDate(Calendar.getDate() + 1);
    }
  }

  // loop for each day
  for (index=0; index < DAYS_OF_MONTH; index++)
  {
    if (Calendar.getDate() > index)
    {
	  // get seagate workweek
	  ww = getSeagateWW(Calendar);
	  ww = ww.substring(8,10); 
	
      // to check for first day of week
      week_day = Calendar.getDay();

      // start new row at Sat
      if (week_day == (DAYS_OF_WEEK - 1))
        cal += '<tr bgcolor=#FFFFFF>';
    
      if (week_day != (DAYS_OF_WEEK + 1))
      {
        // SET VARIABLE INSIDE LOOP FOR INCREMENTING PURPOSES
        var day  = Calendar.getDate();

        cal += '<td align=center>';
        if ((day == currentDay) && (month == currentMonth) && (year == currentYear))
          // HIGHLIGHT TODAY'S DATE
          cal += '<b>' + day + '</b>';
        else
          cal += day;
        cal += '</td>';  
      }
    
      // end row at Fri
      if (week_day == (DAYS_OF_WEEK - 2))
      {
	    // fill workweek number
        cal += '<td align=center bgcolor=#EFEFEF>' + ww + '</td>';
	  
	    // end row
        cal += '</tr>';
      }
    }

    // INCREMENTS UNTIL END OF THE MONTH
    Calendar.setDate(Calendar.getDate()+1);

  }// end for loop

  // see if last day is not Fri
  if ((week_day < DAYS_OF_WEEK)&&(week_day != 5))
  {
	day = 1;
    // see if last day is Sat
    if (week_day == 6) week_day = -1;
  
    // fill overlab day of next month until last day of week
    for (index = week_day+1; index < DAYS_OF_WEEK; index++) {
	  if (index == (DAYS_OF_WEEK-1)) {
        // fill workweek number
        cal += '<td align=center bgcolor=#EFEFEF>' + ww + '</td>';
	  }
	  else {
        show_day = (display_overlap_day) ? day : '';
        cal += '<td align=center><font color="gray">' + show_day + '</font></td>';
        day++;
      }
    }
  
    // new row
    cal += '</tr>';
  }
  return cal;
}
// end getCalendarBody

function getCalendar(display_overlap) {
  display_overlap_day = display_overlap;	
  cal = '<div id="calendarID">';
  cal += '<table bgcolor=#CCCCCC cellpadding=0 cellspacing=0 border=0><tr><td>';
  cal += '<table bgcolor=#CCCCCC cellpadding=0 cellspacing=1 border=0 width=100%>';
  cal += '<tr bgcolor=#6699CC>';
  cal += '<td align="center"><a href=\"javascript:lastMonth();\"><font color="white"><</font></a></td>';
  cal += '<td nowrap align=center colspan=6 height=20><font color=#FFFFFF><b>';
  cal += getMonthName(month) + ' ' + year;
  cal += '</b></font></td><td align="center"><a href=\"javascript:nextMonth();\"><font color="white">></font></a></td>';
  cal += '</tr>';

  // print a day of week
  cal += getDayTitle();

  // print body
  cal += getCalendarBody();

  cal += '</table></td></tr></table>';
  cal += '</div>';
  return cal;
}
// end getCalendar

// End -->
